<?php

    function GetOptInstructionValue($instruction)
    {
        return;
        global $mikron, $php_code_for_eval, $php_file_for_eval;
        $operating_instructions = $mikron->operating_instructions;
        $e = null;
        if(is_null($e))
        {
            $e = $mikron->Queries->Query("T_VARIABLES");
        }
        $match = $instruction;
        $match2 = strtoupper($match);
        $type = 0;
        $value = null;
        while($variable = $e->fetch())
        {
            $code = strtoupper($variable->code->value);
            if($code == $match2)
            {
                $type = $variable->type->value;
                $value = $variable->textvalue->value;
            }
        }
        if(is_null($value))
        {
            if(($pos = strpos($match, ':')) !== false)
            {
                $tp = explode(':', $match);
                $value = $tp[1];
                $cur_instruction = strtolower($tp[0]);
                switch($cur_instruction)
                {
                    case 'function':
                        $type=3;
                        break;
                    case 'int':
                        $type=2;
                        break;
                    case "text":
                        $type=1;
                        break;
                    case "html":
                        $type=4;
                        break;
                    case "file":
                        $type=6;
                        break;
                    case "php":
                        $type=5;
                        break;
                    default:
                        reset($operating_instructions);
                        if(array_key_exists($cur_instruction, $operating_instructions))
                        {
                            $instruction = $operating_instructions[$cur_instruction];
                            $instruction = str_replace("{1}", "\"".$value."\"", $instruction);
                            $php_code_for_eval = $instruction;
                            $value = catchfunction('eval2');
                            $type = 999;
                            break;
                        }
                        $type = 0;
                        $value = null;
                        break;
                }
            }
        }
        if($type > 0)
        {
            switch($type)
            {
                case 999:
                    break;
                case 1: // текст/text
                    break;                            
                case 2: // число/numeric
                    $value = (int)$value;
                    break;
                case 3: // функция/function
                    // нельзя допускать многократного выполнения функции, например чтобы нельзя
                    // было 5 раз сыграть в игру или 5 раз обменять
                    if($iterations > 2)
                    {
                        $breplace = false;
                    }
                    else
                    {
                        $functionname = $value;
                        $with_arguments = false;
                        $pos = strpos($functionname, "(");
                        if($pos!==false)
                        {
                            $functionname = substr($value, 0, $pos);
                            $with_arguments = true;
                        }
                        if(function_exists($functionname))
                        {
                            if($with_arguments)
                            {
                                $php_code_for_eval = $value.";";
                                $value = CatchFunction("eval2");
                            }
                            else
                            {
                                $value = CatchFunction($functionname);
                            }
                        }
                        else
                        {
                            $value = "<font class=\"errortext\">Функция $functionname() не найдена.</font>";
                        }
                    }
                    break;
                case 4: // HTML код/code
                    break;
                case 5: // PHP код/code
                    $php_code_for_eval = $value;
                    $value = catchfunction("eval2");
                    break;
                case 6: // PHP файл
                    $value = getPhpFile($value);
                    break;
            }
            return $value;
        }
    }

    function getPhpFile($parameters)
    {
        global $php_file_for_eval;
        $path = $parameters['path'];
        $php_file = constant('CURRENTSITE_DIR').'/'.$path;
        try
        {
            require_once($php_file);
        }
        catch(Exception $ex)
        {
            return $ex->getMessage();
        }
    }

    function translateOpIns($miniBB_gzipper_in, $html)
    {
        global $mikron;
        static $smarty;     
        /* Smarty */
        // для игнора нужно юзать инструкцию {literal}
        if(is_null($smarty))
        {

            $smarty = $mikron->TemplateMaker->smarty;
            foreach($mikron->OperatingInstructions->All() as $code => $instruction)
            {
                $type = $instruction['type'];
                $processor = $instruction['value'];
                $object = $instruction['object'];
                switch($type)
                {
                    case 'function':
                        $smarty->register_function($code, $processor, $object);
                        break;
                    case 'variable':
                        $smarty->assign($code, $processor);
                        break;
                }
            }

            $variables = Array();
            if(class_exists('T_VARIABLES'))
            {
                try
                {
                    $variables = $mikron->Queries->Query('T_VARIABLES');
                }
                Catch(Exception $ex)
                {
                    $variables = Array();
                }
            }

            if(count($variables) > 0)
            {
                while($var = $variables->fetch())
                {
                    $code = $var->code->value;
                    $type = $var->type->value;
                    $value = $var->textvalue->value;
                    if($type == 3) // function
                    {
                        $smarty->register_function($code, $value);
                    }
                    elseif($type == 6) // php-file
                    {
                        // $smarty->register_function($code, $value);
                    }
                    else
                    {
                        $smarty->assign($code, $value);
                    }
                }
            }
            $smarty->assign('MikronCSS', $mikron->CSS->Content());
            $smarty->register_function('file', 'getPhpFile');
            $smarty->register_function('table', 'ShowEntityAsTable', $mikron->EntityManager);
            $smarty->register_object('MikronConstants', $mikron->Constants);
            $smarty->register_object('mikron', $mikron);
        }

        if(($pos = strpos($html, '{{$BODY}}')) !== false)
        {
            $smarty->assign('BODY', $miniBB_gzipper_in);
        }

        $smarty->assign('template', $html);
        $result = $smarty->fetch('debug.tpl');
        return $result;
    }

?>